package p49_事件发布器;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import p48_事件监听器._1_基于接口的监听器.EmailEvent;
import p48_事件监听器._1_基于接口的监听器.SmsEvent;

/**
 * 这一节介绍了事件发布器原理，同事也介绍了事件监听器原理
 */
@Configuration
@Slf4j
public class _事件发布器原理 {

    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(_事件发布器原理.class);
        context.publishEvent(new SmsEvent("来源于main..."));
        context.publishEvent(new EmailEvent("来源于main..."));
        context.close();
    }

    /**
     * 注册自定义事件发布器，bean名称必须是applicationEventMulticaster
     */
    @Bean
    public _自定义广播器 applicationEventMulticaster() {
        return new _自定义广播器();
    }

    /**
     * 先定义两个监听器
     */
    @Component
    static class SmsListener implements ApplicationListener<SmsEvent> {
        @Override
        public void onApplicationEvent(SmsEvent event) {
            log.info("正在发送短信...");
            log.info("发送短信成功!!!");
        }
    }
    @Component
    static class EmailListener implements ApplicationListener<EmailEvent> {
        @Override
        public void onApplicationEvent(EmailEvent event) {
            log.info("正在发送邮件...");
            log.info("发送邮件成功!!!");
        }
    }
}
